<template>
  <div>
    <el-form ref="editPram" :model="editPram" label-width="130px">
      <div>
        <el-form-item
          label="名称"
          prop="name"
          :rules="[{ required:true,message:'请输入名称',trigger:['blur','change'] }]"
        >
          <el-input v-model="editPram.name" maxlength="20" placeholder="名称" />
        </el-form-item>
        <el-form-item label="父级">
          <el-cascader v-model="editPram.pid" :options="parentOptions" clearable :props="categoryProps" style="width:100%" />
          <!-- allTreeList or parentOptions-->
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number v-model="editPram.sort" :min="0"/>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="editPram.status"  active-text="显示"
                     inactive-text="隐藏" :active-value="1" :inactive-value="0" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loadingBtn" @click="handlerSubmit('editPram')">确定</el-button>
          <el-button @click="close">取消</el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>
<script>
import * as categoryApi from '@/api/categoryApi.js'
import * as roleApi from '@/api/role.js'
export default {
  // name: "edit"
  props: {
    prent: {
      type: Object,
      required: true
    },
    isCreate: {
      type: Number,
      default: 0
    },
    editData: {
      type: Object
    },
    allTreeList: {
      type: Array
    }
  },
  data() {
    return {
      loadingBtn: false,
      constants: this.$constants,
      editPram: {
        name: '',
        pid: null,
        sort: 0,
        status: 1,
        id: 0
      },
      categoryProps: {
        value: 'id',
        label: 'name',
        children: 'child',
        expandTrigger: 'hover',
        checkStrictly: true,
        emitPath: false
      },
      parentOptions: [],
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideEditDialog')
    },
    initEditData() {
      this.parentOptions = [...this.allTreeList]
      this.addTreeListLabelForCasCard(this.parentOptions, 'child')
      const { name, pid, sort, status, id} = this.editData
      if(this.isCreate === 1){
        this.editPram.name = name
        this.editPram.pid = pid
        this.editPram.sort = sort
        this.editPram.status = status
        this.editPram.id = id
      }else{
        this.editPram.pid = this.prent.id
      }
    },
    //处理哪一级别禁用
    addTreeListLabelForCasCard(arr, child) {
        arr.forEach((o,i) => {
          if (o[child] && o[child].length) {
            o[child].forEach((j) => {
              j.disabled = true
            })
          }
        })
    },
    handlerSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (!valid) return
        this.handlerSaveOrUpdate(this.isCreate === 0)
      })
    },
    handlerSaveOrUpdate(isSave) {
      if (isSave) {
        this.editPram.pid = Array.isArray(this.editPram.pid) ? this.editPram.pid[0] : this.editPram.pid
      }
      this.loadingBtn = true
      categoryApi.saveCategory(this.editPram).then(data => {
        this.$emit('hideEditDialog')
        this.$message.success('保存成功')
        this.loadingBtn = false
      }).catch(() => {
        this.loadingBtn = false
      })
    }
  }
}
</script>

<style scoped>

</style>
